<?php

    class Objects_model extends CI_Model{

        var $page_TYPES = array('page','arrangement');
        private $fields = array('post_title','content');

        function __construct(){
            parent::__construct();
        }

        function create_post(){

            
            //Get translate language
            $cur_translate_lang = $this->translate->getLang();
            $this->translate->setLang('me');

            // Clear html special character from title
            $_POST['post_title'] = str_replace('"', '', $_POST['post_title']); 

            $_POST['parent_ID'] = $_POST['parent_id'];
            $_POST['date_time'] = time();
            $_POST['name'] = $this->translate->getArray($_POST['post_title'], TRUE);

            unset($_POST['post_action']);
            unset($_POST['qtrans_textarea_content']);
            unset($_POST['parent_id']);   
            unset($_POST['save']);

            $this->db->insert('posts', $_POST);

            //Set translate language back again
            $this->translate->setLang($cur_translate_lang);

            if($this->db->affected_rows()>0){
                echo json_encode(array('action'=>true,'msg'=>'Successful insert operation.'));
            }else{
                echo json_encode(array('action'=>false,'msg'=>'Something went wrong. Please try again.'));
            }

        }

        function update_post(){

            //Get translate language
            $cur_translate_lang = $this->translate->getLang();
            $this->translate->setLang('me');

            // Clear html special character from title
            $_POST['post_title'] = str_replace('"', '', $_POST['post_title']);
            
            // Clear html spaces from content
            //$_POST['content'] = str_replace('<p>&nbsp;</p>', '', $_POST['content']);

            $ID = $_POST['post_ID'];
            $_POST['parent_ID'] = $_POST['parent_id'];
            $_POST['modified'] = time();
            $_POST['name'] = $this->translate->getArray($_POST['post_title'], TRUE);

            unset($_POST['post_action']);
            unset($_POST['qtrans_textarea_content']);
            unset($_POST['parent_id']);   
            unset($_POST['post_ID']);   
            unset($_POST['save']); 
            
            /*print_r($_POST); 
            exit;*/


            $this->db->where('ID',$ID);
            $this->db->update('posts', $_POST);

            //Set translate language back again
            $this->translate->setLang($cur_translate_lang);

            if($this->db->affected_rows()>0){
                echo json_encode(array('action'=>true,'msg'=>'Successful edit operation.'));
            }else{
                echo json_encode(array('action'=>false,'msg'=>'Something went wrong. Please try again.'));
            }

        }

        function delete_post($post_ID){

            // First Unlink gallery
            /*$res = $this->db->get_where('gallery',array('posts_ID'=>$post_ID))->row_array();

            if(isset($res['ID'])){
               $this->db->where('ID',$res['ID']);
               $this->db->update('gallery',array('posts_ID'=>NULL)); 
            }
            
            // Unlink documents second
            $res = $this->db->get_where('folders',array('posts_ID'=>$post_ID))->row_array();
            if(isset($res['ID'])){
               $this->db->where('ID',$res['ID']);
               $this->db->update('folders',array('posts_ID'=>NULL));
            }*/ 

            //Delete post
            $this->db->where('ID',$post_ID);
            $this->db->delete('posts');

            if($this->db->affected_rows()>0){
                return TRUE;
            }else{
                return TRUE;
            }

        }

        function view_all_objects(){

            if(!isset($_GET['post_type']))$_GET['post_type']='objects';

            $res = $this->db->get('accommodations')->result_array();

            
            
            foreach($res as $key=>$value){

                //Translate class
                foreach($this->fields as $f){
                    $res[$key][$f] = $this->translate->getArray($value[$f], TRUE);
                    if($res[$key][$f]=='')$res[$key][$f]='-Please translate-';                    

                }  

            }
            
            return $res;

        }

        function view_edit_posts(){

            $this->db->where('ID',$_GET['post_ID']);
            $this->db->where('post_type',$_GET['post_type']);
            $res = $this->db->get('posts')->row_array();
            return $res;

        }

        function view_related_post($post_TYPE){ // Page, arrangements, cars etc...

            $this->db->where('post_status','publish');
            $this->db->where('post_type',$post_TYPE);
            $this->db->where('parent_id',0);
            RETURN $this->db->get('posts')->result_array();

        }

        function get_page_types(){
            RETURN $this->page_TYPES;
        }

        function get_fields(){
            RETURN $this->fields;
        }
        
        
        function view_all_posts($post_type){

            $this->db->where('post_type',$post_type);
            $this->db->where('parent_id',0);
            $res = $this->db->get('posts')->result_array();

            
            
            foreach($res as $key=>$value){

                //Translate class
                foreach($this->fields as $f){
                    $res[$key][$f] = $this->translate->getArray($value[$f], TRUE);
                    if($res[$key][$f]=='')$res[$key][$f]='-Please translate-';                    

                }  

            }
            
            return $res;

        }
        
        public function view_post_children($parent_ID, $post_type){
            
            $this->db->where('post_type',$post_type);
            $this->db->where('parent_id',$parent_ID);
            $res = $this->db->get('posts')->result_array();

            
            
            foreach($res as $key=>$value){

                //Translate class
                foreach($this->fields as $f){
                    $res[$key][$f] = $this->translate->getArray($value[$f], TRUE);
                    if($res[$key][$f]=='')$res[$key][$f]='-Please translate-';                    

                }  

            }
            
            RETURN $res;
            
        }

    }

?>
